package com.ndp.fb.walle.business.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ndp.fb.enums.connectionobject.ConnectionObjectType;
import com.ndp.fb.exception.BusinessException;
import com.ndp.fb.rdb.api.*;
import com.ndp.fb.rdb.model.ConnObjectProductConfig;
import com.ndp.fb.rdb.model.ConnectionObject;
import com.ndp.fb.rdb.model.ProductConfiguration;
import com.ndp.fb.util.ListUtil;
import com.ndp.fb.walle.business.ConnectionObjectService;
import com.ndp.fb.walle.model.bo.connectionobject.ConnObjectProductConfigBo;
import com.ndp.fb.walle.model.bo.connectionobject.ConnectionObjectBo;
import com.ndp.fb.walle.model.bo.productconfiguration.result.ProductConfigurationBo;
import com.ndp.fb.walle.util.ConversionUtils;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by infi on 2015/1/29.
 */
@Component
public class ConnectionObjectServiceImpl implements ConnectionObjectService {
    private final org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());

    @Reference
    private ConnectionObjectRdbService connectionObjectRdbService;

    @Reference
    private BusinessManagerRdbService businessManagerRdbService;

    @Reference
    private EntrustAccountConnectionObjectsRdbService entrustAccountConnectionObjectsRdbService;

    @Reference
    private AdPublishConditionRdbService adPublishConditionRdbService;

    @Reference
    private UserRdbServices userRdbServices;

    @Override
    public ConnectionObjectBo findByFbConnectionObjectIdAndType(Long connectionobjectId, Integer type) throws BusinessException {
        ConnectionObject connectionObject = connectionObjectRdbService.findByFbConnectionObjectIdAndType(connectionobjectId, type);
        ConnectionObjectBo connectionObjectBo = new ConnectionObjectBo();
        ConversionUtils.conversion(connectionObject, connectionObjectBo);
        return connectionObjectBo;
    }


    @Override
    public List<ConnObjectProductConfigBo> searchAppsAndConfigurations(String userId) {
        List<ConnObjectProductConfig> connObjectProductConfigs = connectionObjectRdbService.findConnObjectProductConfigdByUserIdAndType(userId, ConnectionObjectType.Application.getType());
        //Po转Bo
        return connObjectProductConfigsPoToBo(connObjectProductConfigs);
    }

    private List<ConnObjectProductConfigBo> connObjectProductConfigsPoToBo(List<ConnObjectProductConfig> connObjectProductConfigs) {
        if (CollectionUtils.isEmpty(connObjectProductConfigs)) {
            return null;
        }
        List<ConnObjectProductConfigBo> connObjectProductConfigBoes = new ArrayList<ConnObjectProductConfigBo>();
        ConnObjectProductConfigBo connObjectProductConfigBo;
        List<ProductConfigurationBo> configurations;
        ConnectionObject connectionObject;
        for (ConnObjectProductConfig connObjectProductConfigPo : connObjectProductConfigs) {
            connObjectProductConfigBo = new ConnObjectProductConfigBo();
            connectionObject = connObjectProductConfigPo.getConnectionObject();
            connObjectProductConfigBo.setId(connectionObject.getId());
            connObjectProductConfigBo.setFbConnectionObjectId(connectionObject.getFbConnectionObjectId());
            connObjectProductConfigBo.setType(connectionObject.getType());
            connObjectProductConfigBo.setName(connectionObject.getName());
            connObjectProductConfigBo.setNativeAppStoreIds(connectionObject.getNativeAppStoreIds());
            connObjectProductConfigBo.setNativeAppTargetingIds(connectionObject.getNativeAppTargetingIds());
            connObjectProductConfigBo.setPicture(connectionObject.getPicture());
            connObjectProductConfigBo.setSupportedPlatforms(connectionObject.getSupportedPlatforms());
            connObjectProductConfigBo.setObjectStoreUrls(connectionObject.getObjectStoreUrls());
            connObjectProductConfigBo.setUrl(connectionObject.getUrl());
            connObjectProductConfigBo.setLogoUrl(connectionObject.getLogoUrl());
            connObjectProductConfigBo.setIconUrl(connectionObject.getIconUrl());

            //配置转BO
            configurations = productConfigurationsPoToBo(connObjectProductConfigPo.getProductConfiguration());
            connObjectProductConfigBo.setConfigurations(configurations);
            connObjectProductConfigBoes.add(connObjectProductConfigBo);
        }

        return connObjectProductConfigBoes;
    }

    private List<ProductConfigurationBo> productConfigurationsPoToBo(List<ProductConfiguration> productConfigurations) {
        List<ProductConfigurationBo> productConfigurationBoes = new ArrayList<ProductConfigurationBo>();
        ProductConfigurationBo productConfigurationBo;
        if (ListUtil.isNotEmpty(productConfigurations)) {
            for (ProductConfiguration productConfigurationPo : productConfigurations) {
                productConfigurationBo = new ProductConfigurationBo();
                ConversionUtils.conversion(productConfigurationPo, productConfigurationBo);
                productConfigurationBoes.add(productConfigurationBo);
            }
        }


        return productConfigurationBoes;
    }



    /**
     * @param userId
     * @return
     * @discription
     * @author giser.yang
     * 创建时间 2015年6月3日 下午4:39:04
     */

    @Override
    public List<ConnectionObject> findByUserIdAndType(String userId, Integer type) {
        // TODO Auto-generated method stub
        return connectionObjectRdbService.findByUserId(userId, type);
//        if (CollectionUtils.isEmpty(connectionObjects)) {
//            return new ArrayList<>();
//        }
//        List<ConnectionObjectBo> connectionObjectBos = new ArrayList<>();
//        for (ConnectionObject connectionObject : connectionObjects) {
//            ConnectionObjectBo connectionObjectBo = new ConnectionObjectBo();
//            ConversionUtils.conversion(connectionObject, connectionObjectBo);
//            connectionObjectBos.add(connectionObjectBo);
//        }
//        return connectionObjectBos;
    }

    @Override
    public List<ConnectionObject> findOwnerConnObjByUserId(String userId)
    {
        return connectionObjectRdbService.findOwnerConnObjByUserId(userId);
    }

    @Override
    public List<ConnectionObject> findByOrgIdAndType(Long orgId, int type) {
        return connectionObjectRdbService.findByOrgIdAndType(orgId, type);
    }


	@Override
	public List<ConnectionObject> findByFbAccountId(Long fbAccountId) {
		return connectionObjectRdbService.findByFbAccountId(fbAccountId);
	}
}
